home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Pict Tricks / CLUTLess / CLUTLessFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-28  |  4.7 KB  |  183 lines  |  [TEXT/MMCC]

  1. /*     
  2.                             CLUTLess
  3.                             
  4.             In some instances it is desireable to store picts stripped of
  5.             CLUTs in order to save some room in the disk. This sample shows
  6.             how to create such picts and how to properly display them back.
  7.             
  8.             SimpleInC.c -- initialization stuff and event loop
  9.             CLUTLess.c  -- code that does the creation of the clut less pict;
  10.                            shows also how to display the pictures.
  11.             CLUTLessFile.c --
  12.                            Routines to access files that have no direct
  13.                            relationship with picture handling.
  14.     
  15. */
  16. #include "CLUTLessFile.h"
  17.  
  18. /* =============================================================================================== */
  19. /* =============================================================================================== */
  20. /* Procs used to save the pict file and the clut
  21. /* =============================================================================================== */
  22.  
  23. /* The pict does not have a color table in its pixmap so here we save
  24.    the clut and we'll use it later when displaying the image.
  25. */
  26. void SaveCLUTResource(SFReply        *sfr)
  27. {
  28. short resNum, currRes;
  29.  
  30.     CreateResFile((sfr)->fName);
  31.     if (resNum = ResError()) return;
  32.  
  33.     currRes = CurResFile();
  34.     
  35.     if ( (resNum = (HOpenResFile((sfr)->vRefNum, 0, (sfr)->fName, fsRdWrPerm))) == -1) {
  36.       resNum = ResError();
  37.       goto bail;
  38.     }
  39.     else {
  40.       UseResFile(resNum);
  41.       AddResource((Handle)gSharedClut, 'clut', 128, "\p");
  42.       CloseResFile(resNum);
  43.     }
  44. bail:
  45.     UseResFile(currRes);
  46. }
  47.  
  48. short OpenPictureFile(SFReply *reply) /* returns refNum for file */
  49. {
  50. long myCount = 4;
  51. long myZero = 0;
  52. short ref = 0,result = 0, i;
  53.  
  54.     if ( Create(reply -> fName, reply -> vRefNum, '????', 'PICT') ) goto bail;
  55.  
  56.     if ( FSOpen(reply -> fName, reply -> vRefNum, &ref) )
  57.       {
  58.         FSDelete(reply -> fName,reply -> vRefNum);
  59.         goto bail;
  60.       }
  61.  
  62.     for (i = 1; i<= (532/4); i++)
  63.       {
  64.         if ( FSWrite(ref, &myCount, (Ptr)&myZero) )
  65.           {
  66.               KillFile(ref,reply);
  67.             goto bail;
  68.           }
  69.       }
  70.       
  71.     if ( SetFPos( ref, fsFromStart, 512) )
  72.       {
  73.           KillFile(ref,reply);
  74.         goto bail;
  75.       }
  76.     result = ref;
  77. bail:
  78.     return result;
  79. }
  80.  
  81. void KillFile(short ref, SFReply *reply)
  82. {
  83.     FSClose(ref);
  84.     FSDelete(reply -> fName,reply -> vRefNum);
  85.     return;
  86.  
  87. }
  88.  
  89.  
  90. /*    Before closing we need to write to disk the header of the picture */
  91. void ClosePictureFile(short ref, SFReply *reply, PicHandle aPict)
  92. {
  93. long pictHeaderSize = 10;
  94.         if ( SetFPos( ref, fsFromStart, 512) )
  95.           {
  96.               KillFile(ref,reply);
  97.             return;
  98.           }
  99.         HLock((Handle) aPict);
  100.         if ( FSWrite( ref, (long *)&pictHeaderSize, (Ptr)*aPict) )
  101.           {
  102.               KillFile(ref,reply);
  103.             return;
  104.           }
  105.         HUnlock((Handle) aPict);
  106.  
  107.         if ( FSClose( ref) )
  108.           {
  109.               KillFile(ref,reply);
  110.             return;
  111.           }
  112. }
  113.  
  114. /* =============================================================================================== */
  115. /* =============================================================================================== */
  116. /* Procs used to get back the pict file and the clut
  117. /* =============================================================================================== */
  118.  
  119. /*     Now we read in the picture and its clut                                                    */
  120. Boolean     GetPictBack(void) {
  121.     Point        dlgPos = {100,100};                /* Position the dialog box */
  122.     SFReply        sfr;                            /* StdFile reply */
  123.     SFTypeList    tList = {'PICT'};                /* only do pict files */
  124.     short         fileRef;
  125.     Boolean     result = false;
  126.     long        curEOF;
  127.     PicHandle    pH;
  128.     
  129.     /* Prompt the user for file name to read from */
  130.     SFGetFile(dlgPos, nil, nil, 1, tList, nil, &sfr);    
  131.     if (sfr.good) {
  132.       if (!(FSOpen(sfr.fName,sfr.vRefNum, &fileRef)) ) {
  133.         if (!(GetEOF(fileRef, &curEOF) )) {
  134.           curEOF -= 512;                    // jump over pict file header
  135.           if ( !(SetFPos( fileRef, fsFromStart, 512)) ) {
  136.             if ( pH = (PicHandle)NewHandle(curEOF) ) {    // allocate space for picture
  137.               HLock((Handle)pH);
  138.               if ( FSRead(fileRef, &curEOF, *pH) ) {
  139.                 HUnlock((Handle)pH);
  140.                 DisposeHandle((Handle)pH);
  141.               }
  142.               else {
  143.                 HUnlock((Handle)pH);
  144.                 gModPict = pH;
  145.                 result = true;
  146.                 GetClutBack(&sfr);
  147.               }
  148.             }
  149.           }
  150.         }
  151.         FSClose(fileRef);
  152.       }      
  153.     }    
  154.     return result;
  155. }
  156. /* =============================================================================================== */
  157. /*    To allow the app to be used to view normal pictures it is going to 
  158.      continue even if the 'clut' is not found in the file.
  159. */
  160.  
  161. void     GetClutBack(SFReply    *sfr) {
  162. short     resNum, currRes;
  163. Handle    resH;
  164.  
  165.     currRes = CurResFile();
  166.     gSharedClut = nil;                // no cheating allowed
  167.     
  168.     if ( (resNum = (HOpenResFile((sfr)->vRefNum, 0, (sfr)->fName, fsRdPerm))) == -1) {
  169.       resNum = ResError();
  170.       goto bail;
  171.     }
  172.     else {
  173.       UseResFile(resNum);
  174.       if ( resH = GetResource('clut', 128) ) {
  175.           DetachResource(resH);
  176.         gSharedClut = (CTabHandle) resH;
  177.       }
  178.       CloseResFile(resNum);
  179.     }
  180. bail:
  181.     UseResFile(currRes);
  182. }
  183.